package com.mayavi.action;

import java.util.ArrayList;
import java.util.List;

import com.mayavi.bc.ICelebrityBc;
import com.mayavi.bo.CelebrityBo;
import com.mayavi.bo.CelebrityTypeBo;
import com.mayavi.dto.CelebrityDto;
import com.mayavi.util.CopyUtil;

public class CelebrityCrudAction {
	private Integer id;
	private CelebrityDto celebrityDto;
	private ICelebrityBc celebrityBc;
	private List<CelebrityBo> celebrities;
	private List<CelebrityTypeBo> celebrityTypes;
	private String action;
//	static SimpleDateFormat format = new SimpleDateFormat("MM/DD/YYYY");
	
	public String execute() {
		if ("save".equals(action)) {
			return save();
		} else if ("list".equals(action)) {
			return list();
		} else if ("edit".equals(action)) {
			return edit();
		}
		return list();
	}
	
	public String list() {
		setCelebrities(celebrityBc.getAllCelebrities());
		return "LIST";
	}

	public String edit() {
		if (id != null && id > 0) {
			CelebrityBo celebrityBo = celebrityBc.getCelebrityById(id);
			if (celebrityBo != null) {
				celebrityDto = new CelebrityDto();
				CopyUtil.copy(celebrityBo, celebrityDto);
//				celebrityDto.setId(id);
//				celebrityDto.setFirstName(celebrityBo.getFirstName());
//				celebrityDto.setMiddleName(celebrityBo.getMiddleName());
//				celebrityDto.setLastName(celebrityBo.getLastName());
//				celebrityDto.setAlias(celebrityBo.getAlias());
//				celebrityDto.setDob(celebrityBo.getDob());
				List<Integer> celebrityTypeIds = new ArrayList<Integer>();
				for(CelebrityTypeBo celebrityTypeBo:celebrityBo.getCelebrityTypes()){
					celebrityTypeIds.add(celebrityTypeBo.getId());
				}
				celebrityDto.setTypes(celebrityTypeIds.toArray(new Integer[]{}));
			}
		}
		setCelebrityTypes(celebrityBc.getAllCelebrityTypes());
		return "EDIT";
	}

	public String save() {
		if (celebrityDto.getId() == null) {
			celebrityDto.setCreatedById(1);
		}
		celebrityDto.setModifiedById(1);

		celebrityBc.saveCelebrity(celebrityDto);
		return "REDIRECT";
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public CelebrityDto getCelebrityDto() {
		return celebrityDto;
	}

	public void setCelebrityDto(CelebrityDto celebrityDto) {
		this.celebrityDto = celebrityDto;
	}

	public ICelebrityBc getCelebrityBc() {
		return celebrityBc;
	}

	public void setCelebrityBc(ICelebrityBc celebrityBc) {
		this.celebrityBc = celebrityBc;
	}

	public List<CelebrityBo> getCelebrities() {
		return celebrities;
	}

	public void setCelebrities(List<CelebrityBo> celebrities) {
		this.celebrities = celebrities;
	}

	public List<CelebrityTypeBo> getCelebrityTypes() {
		return celebrityTypes;
	}

	public void setCelebrityTypes(List<CelebrityTypeBo> celebrityTypes) {
		this.celebrityTypes = celebrityTypes;
	}

}
